<html>
<head><meta charset="utf-8"><title>todo in impl Trait · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html">todo in impl Trait</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227998377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/227998377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#227998377">(Feb 26 2021 at 21:37)</a>:</h4>
<p>Is it expected that something like this does not compile?:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">todo!</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227998751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/227998751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#227998751">(Feb 26 2021 at 21:40)</a>:</h4>
<p>that is expected</p>



<a name="228000947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228000947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228000947">(Feb 26 2021 at 21:55)</a>:</h4>
<p>And sad to me.</p>



<a name="228013868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228013868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228013868">(Feb 26 2021 at 23:42)</a>:</h4>
<p>I think this is the "fallback" described in <a href="https://github.com/rust-lang/rust/issues/35121">#35121</a>.</p>



<a name="228021074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228021074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228021074">(Feb 27 2021 at 01:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/todo.20in.20impl.20Trait/near/228000947">said</a>:</p>
<blockquote>
<p>And sad to me.</p>
</blockquote>
<p>I don't think there should be a way to forge arbitrary implementors of arbitrary types, consider:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">mod</span> <span class="nn">lib</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">mod</span> <span class="nn">seal</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">pub</span><span class="w"></span>
<span class="w">    </span><span class="k">trait</span><span class="w"> </span><span class="n">NeverImplemented</span><span class="w"> </span>: <span class="nc">seal</span>::<span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">ub</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span>::<span class="n">core</span>::<span class="n">hint</span>::<span class="n">unreachable_unchecked</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">foo</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">  </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="n">lib</span>::<span class="n">NeverImplemented</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">todo!</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">exploit</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">helper</span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">foo</span><span class="p">())</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">None</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="c1">// where</span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">helper</span><span class="o">&lt;</span><span class="n">F</span><span class="w"> </span>: <span class="nc">lib</span>::<span class="n">NeverImplemented</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">_</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">F</span><span class="o">&gt;</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">F</span>::<span class="n">ub</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>That being said, I agree that <em>most</em> traits out there are mainly "functional" interfaces (<em>e.g.</em>, <code>Display</code>). So a nice thing to have would be that, when <code>impl</code>ementing a trait for an empty / uninhabited type, providing an implementation for the methods not be needed (since those could always feature a <code>match self {}</code> body). This seems like a pretty sensible thing to do. And then most "functional interfaces" traits would have the courtesy of having been implemented for <code>!</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Display</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Debug</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Error</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="c1">// etc.</span>
</code></pre></div>



<a name="228023975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228023975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228023975">(Feb 27 2021 at 01:53)</a>:</h4>
<p>That code seems pretty pathological...</p>



<a name="228024206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228024206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228024206">(Feb 27 2021 at 01:57)</a>:</h4>
<p>Yeah, <code>!</code> can't automatically implement <em>all</em> traits -- <code>Default</code> being the usual example.</p>
<p>Maybe with some limitations, though.  "Things that are object-safe" might be pretty close, though it's probably not strictly necessary and might not be sufficient either.</p>



<a name="228035026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228035026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228035026">(Feb 27 2021 at 05:29)</a>:</h4>
<p>But isn't it quite useful? I do this (todo macro or loop) when minimizing code. What's the alternative?</p>



<a name="228040543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228040543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228040543">(Feb 27 2021 at 07:24)</a>:</h4>
<p>One day™ <code>!</code> will stabilize and <code>impl Foo for ! {}</code> will cover many of the cases.  Dunno about the rest.</p>



<a name="228041492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228041492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228041492">(Feb 27 2021 at 07:46)</a>:</h4>
<p>i'm surprised by this since i was under the impression that were cases where the compiler turns the body of all functions into <code>loop {}</code> (the everybody_loops pass/pprint option). i guess either that doesnt work here (or anymore), or its more involved than i knew.</p>



<a name="228042105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/todo%20in%20impl%20Trait/near/228042105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/todo.20in.20impl.20Trait.html#228042105">(Feb 27 2021 at 08:01)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> it has to explicitly skip these cases:<br>
<a href="https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_interface/util.rs.html#702">https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_interface/util.rs.html#702</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>